
package servlet;

import dao.UsuarioDAO;
import entidade.Usuario;
import java.io.IOException;
import java.io.PrintWriter;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.UserTransaction;

/**
 *
 * @author luiz
 */
public class UsuarioServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String nome = request.getParameter("nome");
        String email = request.getParameter("email");
        String acao = request.getParameter("acao");
        String id = request.getParameter("id");

        Usuario usuario = new Usuario();
        usuario.setEmail(email);
        usuario.setNome(nome);
        UsuarioDAO dao = new UsuarioDAO();
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            boolean cadastrou = true;
            if (acao!=null && !acao.isEmpty()&& acao.equals("UsuarioCadastro") && nome != null && email != null && !nome.equals("") && !nome.isEmpty() && !email.equals("") && !email.isEmpty()) {
                try {

                    for (Usuario saidaUsuario : dao.buscar(Usuario.class)) {
                        if (email.equals(saidaUsuario.getEmail())) {
                            cadastrou = false;
                            break;
                        }
                    }
                    if (cadastrou) {
                        dao.salvar(usuario);
                    }
                } catch (Exception e) {

                }
            }
            if (acao!=null && !acao.isEmpty()&& acao.equals("apagar") && id != null && !id.equals("") && !id.isEmpty()) {
                try {
                    dao.deletar(dao.buscaID(Usuario.class, Long.parseLong(id)));
                } catch (Exception e) {

                }
            }

            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Produto</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("");
            out.println("<form action='UsuarioServlet' method='get'>");
            
            if (cadastrou) {
                out.println("<label>Nome :</label>");
                out.println("<input type='text' name='nome' value=''/>");
                out.println("<label>Email :</label>");
                out.println("<input type='text' name='email' value=''/>");
            } else {
                out.println("<label>Nome :</label>");
                out.println("<input type='text' name='nome' value='" + nome + "'/>");
                out.println("<label>Email :</label>");
                out.println("<input type='text' name='email' value='' placeholder='Email já cadastrado'/>");
            }
            out.println("</center></br><input type='submit' value='UsuarioCadastro' name='acao'/>");
            out.println("</form><br/></div><br/>");
            out.println("<br/>");
            out.println("<br/>");
            out.println("<ul>");
            for (Usuario saidaUsuario : dao.buscar(Usuario.class)) {
                out.println("<li>");
                out.println(saidaUsuario.getNome());
                out.println("<a href='?id=" + saidaUsuario.getId() + "&acao=apagar'>APAGAR</a>");
                out.println("</li>");
            }
            out.println("</ul>");
            out.println("</body>");
            out.println("</html>");
            
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
